package com.jxb.dp20251355;

import com.jxb.seven.HouseRobberIII_337;

public class HouseRobberIII1_337 {

    public static void main(String[] args) {

    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /*标准DP解法*/
    public int rob(TreeNode root) {
        int[] result = robNode(root);
        return Math.max(result[0],result[1]);
    }

    private int[] robNode(TreeNode root) {
        if (root == null) {
            return new int[]{0,0};
        }

        //(1,0)
        int[] left = robNode(root.left);
        int[] right = robNode(root.right);

        //
        int stealCurr = root.val + left[1] + right[1];

        //1
        int noStealCurr = Math.max(left[0], left[1] + Math.max(right[0],right[1]));
        //(1,0)
        return new int[]{stealCurr,noStealCurr};
    }

}
